package com.jch.dm3_two_phase_termination.extension;

public class TestMain {

    public static void main(String[] args) {
        System.out.println("main:BEGIN");
        // (1) 设置未捕获的异常的处理器
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("****");
                System.out.println("UncaughtExceptionHandler:BEGIN");
                System.out.println("currentThread = " + Thread.currentThread());
                System.out.println("thread = " + t);
                System.out.println("exception = " + e);
                System.out.println("UncaughtExceptionHandler:END");
            }
        });

        // (2) 设置退出钩子
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                System.out.println("****");
                System.out.println("shutdown hook:BEGIN");
                System.out.println("currentThread = " + Thread.currentThread());
                System.out.println("shutdown hook:END");
            }
        });

        // (3) 大约3秒后启动执行“整数除零计算”的线程
        new Thread("MyThread"){
            @Override
            public void run() {
                System.out.println("MyThread:BEGIN");
                System.out.println("MyThread:SLEEP...");
                try{
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("MyThread:DIVIDE");
                // 整数除零计算
                int x = 1/0;
                System.out.println("MyThread:END");
            }
        }.start();
        System.out.println("main:END");
    }

}
